package Verktoy;

import Beans.Deltaker;
import DB.DBehandler;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

/**
 * Klasse som konverterer deltaker mellom string og object
 * @author Gruppe1
 */
@ManagedBean(name = "DeltakerConverter")
public class DeltakerConverter implements Converter{
    
    private DBehandler db = new DBehandler();
    private ArrayList<Deltaker> k;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException{
        String [] tab = value.split(" ");
        k=db.hentAlleDeltakere();
        for(Deltaker a : k)
            if(a.getEpost().equals(tab[tab.length-1])) return a;
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value)throws ConverterException {
        Deltaker d = (Deltaker)value;
        return d.toString();
    }
}
